﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;
using StoragePeriod.Web.UserControls;
using StoragePeriod.Model.RawDict;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.BLL.FoodTesting;
using System.Data;
using StoragePeriod.Web.NopiExcel;

namespace StoragePeriod.Web.Nutritionknowledge
{
    public partial class NutrientsOnTime : SecurityPage
    {

        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作
        T_FoodRecord tFoodRecord = new T_FoodRecord();//原料检测

        public override string PrivCode()
        {
            return "967c9b3589119cc2";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LeftTree.SelectedChanging += new MaterialTree.TreeEventDelegate(LeftTree_SelectedChanging);
            LeftLable.Text = "营养素查询";
            if (IsPostBack) return;
        }

        protected void LeftTree_SelectedChanging(Dictionary<string, string> dic)
        {
            if (dic.Count > 0)
            {
                int[] List = new int[dic.ToList().Count()];
                int i = 0;
                dic.ToList().ForEach(p =>
                {
                    RawInfo s = new RawInfo();
                    s.RawId = Convert.ToInt32(p.Key);
                    List[i] = s.RawId;
                    i++;
                });
                List<RawInfo> RawList=SetDataValue(List);
                this.Rep_Rawinfo.DataSource = RawList;
                this.Rep_Rawinfo.DataBind();
            }
        }

        public List<RawInfo> SetDataValue(int[] List)
        {
            int[] RawIdList = List; //获取选中的所有原料
            List<RawInfo> RawInfoList = new List<RawInfo>();

            E_FoodRecord md = new E_FoodRecord(); //查询实体
            if (this.txtStartTime.Text.Trim() != "")
            {
                md.FoodStartTime = Convert.ToDateTime(this.txtStartTime.Text.Trim());
            }
            if (this.txtEndTime.Text.Trim() != "")
            {
                md.FoodEndTime = Convert.ToDateTime(this.txtEndTime.Text.Trim());
            }

            foreach (int RawId in RawIdList)
            {
                RawInfo info = new RawInfo();
                info.RawId = RawId;
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                if (eRawMaterial != null)
                {
                    info.RawName = eRawMaterial.RAWName;
                }
                //遍历所有的检测 
                for (int i = 1; i <= 15; i++)
                {
                    md.RawID = RawId;
                    md.FoodTypeID = i;
                    E_FoodRecord eFoodRecord = tFoodRecord.GetNewFoodResult(md);
                    if (eFoodRecord != null)
                    {
                        switch (i)
                        {
                            case 1://1：Vc含量测定（VcData）
                                info.Food_Vc = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 2://2：水分含量（WaterData）
                                //info.Food_水分含量 = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 3://3：碳水化合物（CarbohydratesData）
                                info.Food_碳水化合物 = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 4://4：蛋白质含量（ProteinData）
                                info.Food_蛋白质 = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 5:// 5：矿物质（MineralData）锌 该项废除
                                //info.Food_Zn = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 6:// VA
                                info.Food_VA = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 7:// VE
                                info.Food_VE = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 8:// 氨基酸
                                info.Food_氨基酸 = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 9:// 膳食纤维
                                info.Food_膳食纤维 = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 10:// 10：铜 
                                info.Food_Cu = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 11:// 11：铁 
                                info.Food_Fe = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 12:// 12：锌
                                info.Food_Zn = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 13:// 13：钙
                                info.Food_Ca = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 14://  14：镁
                                info.Food_Mg = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                            case 15://  15磷
                                info.Food_P = eFoodRecord.FoodResult + "(" + Convert.ToDateTime(eFoodRecord.FoodTime).ToShortDateString() + ")";
                                break;
                        }
                    }
                }
                RawInfoList.Add(info);
            }
            return RawInfoList;

        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dic = LeftTree.GetCheckedNodes();
            if (dic.Count > 0)
            {
                int[] List = new int[dic.ToList().Count()];
                int i = 0;
                dic.ToList().ForEach(p =>
                {
                    RawInfo s = new RawInfo();
                    s.RawId = Convert.ToInt32(p.Key);
                    List[i] = s.RawId;
                    i++;
                });
                List<RawInfo> RawInfoList = SetDataValue(List);
                this.Rep_Rawinfo.DataSource = RawInfoList;
                this.Rep_Rawinfo.DataBind();
            }

        }

        /// <summary>
        /// 获取初始化原料详细表
        /// </summary>
        /// <returns></returns>
        public DataTable GetRawInfoDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("原料名称");
            dt.Columns.Add("能量（实时）");
            dt.Columns.Add("蛋白质（实时）");
            dt.Columns.Add("脂肪（实时）");
            dt.Columns.Add("碳水化合物（实时）");
            dt.Columns.Add("VC（实时）");
            dt.Columns.Add("还原糖（实时）");
            dt.Columns.Add("总维A（实时）");
            dt.Columns.Add("硫胺素（实时）");
            dt.Columns.Add("核黄素（实时）");
            dt.Columns.Add("总维E（实时）");
            dt.Columns.Add("钾（实时）");
            dt.Columns.Add("钙（实时）");
            dt.Columns.Add("镁（实时）");
            dt.Columns.Add("铁（实时）");
            dt.Columns.Add("锌（实时）");
            dt.Columns.Add("铜（实时）");
            dt.Columns.Add("氨基酸（实时）");
            dt.Columns.Add("膳食纤维（实时）");
            return dt;
        }

        protected void Btn_Export_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dic = LeftTree.GetCheckedNodes();
            if (dic.Count > 0)
            {
                int[] List = new int[dic.ToList().Count()];
                int i = 0;
                dic.ToList().ForEach(p =>
                {
                    RawInfo s = new RawInfo();
                    s.RawId = Convert.ToInt32(p.Key);
                    List[i] = s.RawId;
                    i++;
                });
                List<RawInfo> RawInfoList = SetDataValue(List);
                DataTable RawInfoDt = this.GetRawInfoDt();
                foreach (RawInfo item in RawInfoList)
                {
                    DataRow row = RawInfoDt.NewRow();
                    row["原料名称"] = item.RawName;
                    row["能量（实时）"] = "";
                    row["蛋白质（实时）"] = item.Food_蛋白质;
                    row["脂肪（实时）"] = "";
                    row["碳水化合物（实时）"] = item.Food_碳水化合物;
                    row["VC（实时）"] = item.Food_Vc;
                    row["还原糖（实时）"] = "";
                    row["总维A（实时）"] = item.Food_VA;
                    row["硫胺素（实时）"] = "";
                    row["核黄素（实时）"] = "";
                    row["总维E（实时）"] = item.Food_VE;
                    row["钾（实时）"] = item.Food_K;
                    row["钙（实时）"] = item.Food_Ca;
                    row["镁（实时）"] = item.Food_Mg;
                    row["铁（实时）"] = item.Food_Fe;
                    row["锌（实时）"] = item.Food_Zn;
                    row["铜（实时）"] = item.Food_Cu;
                    row["氨基酸（实时）"] = item.Food_氨基酸;
                    row["膳食纤维（实时）"] = item.Food_膳食纤维;
                    RawInfoDt.Rows.Add(row);
                }
                ExcelRender.RenderToExcel(RawInfoDt, HttpContext.Current, Guid.NewGuid().ToString() + ".xls");
            }
        }
    }
}